package com.carful.v10.service;

import com.alibaba.fastjson.JSON;
import com.carful.v10.entity.HyInfo;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ad
 * Date 2024/7/11 16:10
 */

@Slf4j
@Service
public class V10Service {

    @Autowired
    private RestTemplate restTemplate;

    private String infoUrl = "http://mobile.jfldq.com/SCM/Inbound/invoke.do?interfaceId=myjfl.query.hyxx&rmstoken=127.0.0.1&data={data}";


    public List<HyInfo> getMemberInfo(String phone, String memberId) {

        List<HyInfo> members = new ArrayList<>();

        Map<String, String> hashMap = new HashMap<>();
        if (null != phone) hashMap.put("SJHM", phone);
//        if (null != memberId) hashMap.put("HYMC", memberId);

        ObjectMapper mapper = new ObjectMapper();
        String data = null;
        try {
            data = mapper.writeValueAsString(hashMap);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        String object = restTemplate.getForObject(infoUrl, String.class, data);
        Map allDayMap = null;
        try {
            allDayMap = mapper.readValue(object, Map.class);

            List allDayDataList = (List)allDayMap.get("DATA");
            for (Object o : allDayDataList) {
                String s = mapper.writeValueAsString(o);
                HyInfo hyInfo = JSON.parseObject(s, HyInfo.class);
                members.add(hyInfo);
            }
        } catch (Exception e) {
            log.error("获取会员信息序列化错误");
        }
        return members;
    }

}
